# coding=utf-8
"""
cron: 0 0 6 * * ?
new Env("有道云笔记")
"""
import pymysql
import requests
import utils.db as DB
import utils.tool as TOOL

global msg

msg = """
名称|空间
:--:|:--:
"""
mysql = DB.mysql()


def main():
    dbUser = mysql.select_all("select * from tb_ydybj where status = 0")
    print("🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉\n")
    print("获取到: "+str(len(dbUser))+" 个用户")
    print("-------------------------------------------")

    for user in dbUser:
        signIn(user)


# 签到
def signIn(user):
    global msg

    ad = 0
    payload = 'yaohuo:id34976'
    headers = {'Cookie': user['cookie']}

    re = requests.request(
        "POST", "https://note.youdao.com/yws/api/daupromotion?method=sync", headers=headers, data=payload)
    if 'error' not in re.text:
        res = requests.request(
            "POST", "https://note.youdao.com/yws/mapi/user?method=checkin", headers=headers, data=payload)
        for i in range(3):
            resp = requests.request(
                "POST", "https://note.youdao.com/yws/mapi/user?method=adRandomPrompt", headers=headers, data=payload)
            ad += resp.json()['space'] // 1048576
        if 'reward' in re.text:
            sync = re.json()['rewardSpace'] // 1048576
            checkin = res.json()['space'] // 1048576
            # 签到成功
            print(user['user_name'] + " ---> " +
                  str(sync + checkin + ad) + 'M\n')
            msg += user['user_name'] + "|" + \
                str(sync + checkin + ad) + 'M\n\n'
    else:
        if sever != '0':
            print(user['user_name'] + " ---> " + 'cookie失效\n')
            msg += user['user_name'] + "|cookie失效\n\n"

    print("-------------------------------------------")


main()

# 一定要关闭数据库
print("\n🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉")
mysql.close()


TOOL.msgSend("b5fd9aaa249242e3aea2ca4bbebb6bfa", " ", msg, True)
